package com.StarStudio.Controller;

import com.StarStudio.Service.GoodsStockService;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.Entity.*;
import com.ruoyi.common.core.utils.QueryPageUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
//@RequestMapping("/goods_stock")
public class GoodsStockController {

    @Autowired
    GoodsStockService goodsStockService;

    /**
     * 商品库存分页方法
     * @param goodsStock 商品库存对象
     * @return 分页结果对象 PageResult
     */
    @GetMapping("/page")
    public PageResult Page(GoodsStock goodsStock){
        QueryPageUtils.startPage();
        PageInfo pageInfo;
        List<GoodsStock> goodsStockList;
        long allRecordNumber;
        try {
            goodsStockList = goodsStockService.findGoodsStockList(goodsStock);
            allRecordNumber = goodsStockService.findAllRecordNumber();
            pageInfo = new PageInfo<>(goodsStockList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.QUERY_PAGE_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS,MessageConstant.QUERY_PAGE_SUCCESS, pageInfo.getTotal(), goodsStockList, allRecordNumber);
    }

    /**
     * 导出商品库存详情信息到 Excel表格
     * @param response http响应
     * @param goodsStock 商品库存对象
     */
    @Log(title = "商品库存详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsStock goodsStock) {
        List<GoodsStock> list = goodsStockService.findGoodsStockList(goodsStock);
        ExcelUtil<GoodsStock> util = new ExcelUtil<>(GoodsStock.class);
        util.exportExcel(response, list, "商品库存数据");
    }

    /**
     * 新增商品库存个体接口
     * @param goodsStock 商品库存个体对象
     * @return
     */
    @PostMapping("/post")
    public Result addGoodsStock(@RequestBody GoodsStock goodsStock){
        try {
            goodsStockService.addGoodsStock(goodsStock);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.ADD_GOODS_STOCK_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.ADD_GOODS_STOCK_SUCCESS);
    }

    /**
     * 更新商品库存接口
     * @param goodsStock 商品库存对象
     * @return 结果对象 Result
     */
    @PutMapping("/put")
    public Result updateGoodsStock(@RequestBody GoodsStock goodsStock){
        try {
            goodsStockService.updateGoodsStock(goodsStock);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.UPDATE_GOODS_STOCK_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.UPDATE_COUPONS_STOCK_SUCCESS);
    }

    /*
     *   远程调用接口
     */
    @RequestMapping("/UnsoldItemIds/{id}")
    public List<Long> findUnsoldItemIdsByGoodsId(@PathVariable long id){
        List<Long> unsoldItemIdsByGoodsId = goodsStockService.findUnsoldItemIdsByGoodsId(id);
        if (CollectionUtils.isEmpty(unsoldItemIdsByGoodsId)){
            return null;
        }
        return unsoldItemIdsByGoodsId;
    }


    /*
     *   远程调用接口
     */
    @RequestMapping("/realDelete/{id}")
    public void realDelete(@PathVariable long id){
        goodsStockService.realDelete(id);
    }

    /*
     *   远程调用接口
     */
    @RequestMapping("/SoldItemIds/{id}")
    public List<Long> findSoldItemIdsByGoodsId(@PathVariable long id){
        List<Long> soldItemIdsByGoodsId = goodsStockService.findSoldItemIdsByGoodsId(id);
        if (CollectionUtils.isEmpty(soldItemIdsByGoodsId)){
            return null;
        }
        return soldItemIdsByGoodsId;
    }

    /*
     *   远程调用接口
     */
    @RequestMapping("/updateDelFlag/{id}")
    public void updateDelFlag(@PathVariable long id){
        goodsStockService.updateDelFlag(id);
    }

    /*
     *   远程调用接口
     */
    @RequestMapping("/findGoodsStockIds/{goodsId}/{storeId}")
    List<Long> findGoodsStockIds(@PathVariable long goodsId, @PathVariable long storeId){
        List<Long> goodsStockIds = goodsStockService.findGoodsStockIds(goodsId, storeId);
        if (CollectionUtils.isEmpty(goodsStockIds)){
            return null;
        }
        return goodsStockIds;
    }

    /*
     *   远程调用接口
     */
    @RequestMapping("/UnsoldItems/{goodsId}/{flag}")
    List<GoodsStock> findUnsoldItems(@PathVariable(value = "goodsId") long goodsId,@PathVariable(value = "flag") boolean flag){
        List<GoodsStock> unsoldItems = goodsStockService.findUnsoldItems(goodsId, flag);
        if (CollectionUtils.isEmpty(unsoldItems)){
            return null;
        }
        return unsoldItems;
    }

    /*
     *   远程调用接口
     */
    @RequestMapping("/SoldItems/{goodsId}/{flag}")
    List<GoodsStock> findSoldItems(@PathVariable(value = "goodsId") long goodsId,@PathVariable(value = "flag") boolean flag){
        List<GoodsStock> soldItems = goodsStockService.findSoldItems(goodsId, flag);
        if (CollectionUtils.isEmpty(soldItems)){
            return null;
        }
        return soldItems;
    }

    @RequestMapping("/findByOrderId/{orderId}")
    List<GoodsStock> findByOrder_id(@PathVariable(value = "orderId") long orderId){
        List<GoodsStock> byOrder_id = goodsStockService.findByOrder_id(orderId);
        if (CollectionUtils.isEmpty(byOrder_id)){
            return null;
        }
        return byOrder_id;
    }


}
